package com.atguigu.gmall.service.minio;

import com.atguigu.gmall.service.minioservice.Impl.OSSServiceImpl;
import com.atguigu.gmall.service.minioservice.OSSService;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@EnableConfigurationProperties(MinioProperties.class)
@Configuration
public class MinioAutoConfiguration {
    @Autowired
    MinioProperties minioProperties;
    @Bean
    public MinioClient minioClient() throws Exception {
        MinioClient minioClient = new MinioClient(minioProperties.getEndpoint(),
                minioProperties.getAccessKey(),
                minioProperties.getSecretKey());
        boolean Exists= minioClient.bucketExists(minioProperties.getBucket());
        if (!Exists){
            minioClient.makeBucket(minioProperties.getBucket());
        }

        return minioClient;

    }
    @Bean
    public OSSService ossService(){
        OSSServiceImpl service = new OSSServiceImpl();

        //SpringBoot会自动给OSSServiceImpl注入他想要的
       return service;
    }
}
